package com.co.recommend.controller;

import com.co.common.entity.Result;
import com.co.recommend.service.FeatureService;
import com.co.recommend.service.NewVideoService;
import com.co.recommend.service.PopularVideoService;
import com.co.recommend.service.SubService;
import com.co.video.dto.VideoDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/rec")
/**
 * 推荐视频
 */

public class RecommendController {

    @Autowired
    private FeatureService featureService;
    @Autowired
    private SubService subService;
    @Autowired
    private NewVideoService newVideoService;
    @Autowired
    private PopularVideoService popularVideoService;

    /**
     * 精选视频
     *
     * @return
     */
    @GetMapping("/feature")
    public Result<List<VideoDto>> feature() {
        try {
            List<VideoDto> videoDtos = featureService.find(5, 3, 2, 8);
            return Result.ok(videoDtos);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.failed();
    }

    @PostMapping("/sub/add")
    public Result<String> addSubscription(@RequestParam String userName, @RequestParam String upName) {
        boolean flag = subService.addSub(userName, upName);
        if (!flag) {
            return Result.failed("订阅失败");
        }
        return Result.ok("订阅成功");
    }

    @PostMapping("/sub/del")
    public Result<String> delSubscription(@RequestParam String userName, @RequestParam String upName) {
        boolean flag = subService.removeSub(userName, upName);
        if (!flag) {
            return Result.failed("删除订阅失败");
        }
        return Result.ok("删除订阅成功");
    }


    /**
     * 订阅推送全部的视频
     *
     * @param userName
     * @return
     */
    @GetMapping("/sub/{userName}")
    public Result<List<Object>> subVideo(@PathVariable String userName) {
        List<Object> list = subService.subVideo(userName);

        if (list != null) {
            return Result.ok(list);
        }
        return Result.failed();
    }

    /**
     * 订阅订阅推送的视频  aid=_all 传4个
     *
     * @param userName
     * @return
     */
    @GetMapping("/sub/{userName}/{aid}")
    public Result<Object> subVideo(@PathVariable String userName, @PathVariable String aid) {
        Object subVideo = subService.subVideo(userName, aid);
        if (subVideo != null) {
            return Result.ok(subVideo);
        }
        return Result.failed("查找失败");
    }

    //up主被多少人人订阅
    @GetMapping("/subcount/{userName}")
    public Result<Integer> subCount(@PathVariable String userName) {
        Integer integer = subService.subCount(userName);
        return Result.ok(integer);
    }

    //最新视频
    @GetMapping("/new")
    public Result<List<VideoDto>> findNewVideo() {
        List<VideoDto> newVideos = newVideoService.findNewVideo(8);
        if (newVideos != null) {
            return Result.ok(newVideos);
        }
        return Result.failed();
    }

    /**
     * 流行视频
     *
     * @return
     */
    @GetMapping("/popular")
    public Result<List<VideoDto>> popular() {
        try {
            List<VideoDto> videoDtos = popularVideoService.loadPoupVideoState(1, 7, 2, 8);
            return Result.ok(videoDtos);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.failed();
    }

}
